<template>
  <div class="box">
    <div class="zc">注册功能</div>
    <div>
      <el-card class="box-card">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" prop="uname">
            <el-input
              type="text"
              v-model="ruleForm.uname"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input
              type="password"
              v-model="ruleForm.pass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="验证码">
            <el-input v-model.number="ruleForm.captcha"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="success" @click="submitForm('ruleForm')"  style="margin-right:35px;margin-left:50px"
              >注册</el-button
            >
            <!-- <el-button @click="resetForm('ruleForm')"  style="margin-right:35px">重置</el-button> -->
            <el-button type="primary" @click="$router.push('/login')">去登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import url from '@/config/url'
export default {
  data() {
    // 校验用户名
    var validateUname = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('用户名不能为空'))
      }
      callback()
    }
    // 校验密码
    var validatePass = (rule, value, callback) => {
      if (value.length < 6 || value.length > 12) {
        callback(new Error('请输入6-12位密码'))
      }
      callback()
    }
    return {
      ruleForm: {
        uname: '',
        pass: '',
        captcha: ''
      },
      //  校验规则
      rules: {
        // uname 校验的对应 el-form-item的prop属性
        // validator 定义校验方法
        // trigger 校验时机
        uname: [{ validator: validateUname, trigger: 'blur' }],
        pass: [{ validator: validatePass, trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //   alert('submit!')
          // console.log(this.ruleForm)
          const { pass, uname } = this.ruleForm
          this.$http
            .post(url.register, {
              username: uname,
              password: pass
            })
            .then((res) => {
              if (res.data.code === 0) {
                this.$message({
                  message: '注册成功',
                  type: 'success',
                  duration:1000,
                  onClose: () => {
                    // this.$router.push('/login')
                  }
                })
              } else {
                // console.log(res);
                this.$message({
                  message: '注册失败',
                  type: 'error',
                  duration:1000,
                  
                  onClose: () => {
                    this.$refs[formName].resetFields()
                  }
                })
              }
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      // ref调用子组件方法
      this.$refs[formName].resetFields()
    }
  }
}
</script>
</script>

<style lang="scss" scoped>
.box {
  width: 100vw;
  height: 100vh;
  background-image: url(../assets/images/bg.png);
  background-size: 100% 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  > .zc {
    width: 200px;
    font-size: 32px;
    height: 30px;
    color: transparent;
    text-align: center;
    margin-bottom: 20px;
    margin-top:270px;
    margin-left:100px
  }
  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both;
  }

  .box-card {
    width: 480px;
    background-color: rgba(255,255,255,.6);
    border-radius: 20px;
    margin-left: 400px;
  }
}
</style>